//
//  LYRegisterViewController.m
//  PianoAndPoemDirectSeeding
//
//  Created by 李云 on 2017/7/11.
//  Copyright © 2017年 梅津山. All rights reserved.
//

#import "LYRegisterViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import <AVOSCloud/AVOSCloud.h>
#import <LeanCloudSocial/AVOSCloudSNS.h>
#import "NowController.h"
#import "NowEngineerController.h"

@interface LYRegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *callNum;//电话号
@property (weak, nonatomic) IBOutlet UITextField *num; //验证码
@property(nonatomic,strong)UIActivityIndicatorView *acty;//活动指示器视图
@property (weak, nonatomic) IBOutlet UITextField *maintainID; //维修工号
@property (weak, nonatomic) IBOutlet UIButton *getBtn;


@end

@implementation LYRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _getBtn.layer.cornerRadius = 5;
    _getBtn.layer.borderWidth = 1;
    _getBtn.layer.borderColor = [UIColor blueColor].CGColor;
//    _getBtn.layer.masksToBounds = YES;
    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
}


//返回按钮
- (IBAction)backBtn:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
//获取验证码
- (IBAction)getBtn:(UIButton *)sender {
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_callNum.text zone:@"86" result:^(NSError *error) {
        
        if (!error)
        {
            // 请求成功
            UIAlertController *aletv = [UIAlertController alertControllerWithTitle:@"" message:@"验证码已发送" preferredStyle:    UIAlertControllerStyleAlert];
            [self presentViewController:aletv animated:YES completion:^{
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [aletv dismissViewControllerAnimated:YES completion:^{
                        sender.enabled = NO;
                        _getBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
                    }];
                });
            }];
        }
        else
        {
            UIAlertController *aletv = [UIAlertController alertControllerWithTitle:@"" message:@"验证码获取失败" preferredStyle:    UIAlertControllerStyleAlert];
            [self presentViewController:aletv animated:YES completion:^{
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [aletv dismissViewControllerAnimated:YES completion:^{
                        sender.enabled = YES;
                        _getBtn.layer.borderColor = [UIColor blueColor].CGColor;
                    }];
                });
            }];
            
        }
    }];

}
//登录按钮
- (IBAction)login:(UIButton *)sender {
    
//    if ([_maintainID.text isEqualToString:@"0809"]) {
//        NowEngineerController *now = [NowEngineerController sharedNow];
//        [self presentViewController:now animated:YES completion:nil];
//    }else if ([_maintainID.text isEqualToString:@" "] || _maintainID){
//        NowController *now = [NowController sharedNow];
//        [self presentViewController:now animated:YES completion:nil];
//    }
//
    // 先获得用户手机号，在leancloud里面查询，
    // 有： 使用SMSSDK 验证 - 》 登录
    // 没有： 使用SMSSDK 验证 - 》 再使用leancloud 注册 - 》 登录
//      _maintainID.text = @"0809";
          [SMSSDK commitVerificationCode:_num.text phoneNumber:_callNum.text zone:@"86" result:^(NSError *error) {
    
            if (!error)
            { [AVUser logInWithUsernameInBackground:_callNum.text password:@"123" block:^(AVUser *user, NSError *error) {
                if (user != nil) {
                    if ([_maintainID.text isEqualToString:@"0809"]) {
                        NowEngineerController *now = [NowEngineerController sharedNow];
                        [self presentViewController:now animated:YES completion:nil];
                    }else if ([_maintainID.text isEqualToString:@" "] || _maintainID){
                        
                    NowController *now = [NowController sharedNow];
                    [self presentViewController:now animated:YES completion:nil];
                       
                    
                    }
                } else {
                    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
                    user.username = _callNum.text;// 设置用户名
                    user.password = @"123";// 设置密码
                
                    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                        if (succeeded) {
                            //保存一个微信人员的表
//                            AVObject *xxUser =[AVObject objectWithClassName:@"xxUser"];
//                            [xxUser setObject:@"YES" forKey:@"isGC"];
//                            [xxUser setObject:user forKey:@"user"];
//                            [xxUser saveInBackground];
                            // 注册成功
                            [AVUser logInWithUsernameInBackground:_callNum.text password:@"123"  block:^(AVUser *user, NSError *error) {
                                if (user != nil) {
                                  

                                    if ([_maintainID.text isEqualToString:@"0809"]) {
                                        NowEngineerController *now = [NowEngineerController sharedNow];
                                        [self presentViewController:now animated:YES completion:nil];
                                    }else if ([_maintainID.text isEqualToString:@" "] || _maintainID){
                                       
                                        NowController *now = [NowController sharedNow];
                                        [self presentViewController:now animated:YES completion:nil];
                                        
                                        AVUser *currentUser = [AVUser currentUser];
                                        [currentUser setObject:_callNum.text forKey:@"name"];
                                        UIImage *img = [UIImage imageNamed:@"NO.jpg"];
                                        NSData *data = UIImagePNGRepresentation(img);
                                        NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                        [currentUser setObject:string forKey:@"image"];
                                        [currentUser saveInBackground];
                                        
                                        
                                        }

                                } else {
                                    
                                }
                            }];
                            
                            
                        } else {
                            // 失败的原因可能有多种，常见的是用户名已经存在。
                        }
                    }];
                    
                    
    
                }
            }];
    
    
                       }
            else
            {
    
            }
        }];
    }


@end
